package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.mapper.CategoryMapper;
import com.mszlu.blog.dao.pojo.Category;
import com.mszlu.blog.service.CategoryService;
import com.mszlu.blog.utils.EmptyUtils;
import com.mszlu.blog.vo.CategoryVo;
import com.mszlu.blog.vo.ErrorCode;
import com.mszlu.blog.vo.PageResult;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.CategoryPageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.mszlu.blog.service.impl
 * @ClassName: CategoryServiceImpl
 * @Author: 陈威
 * @CreateTime: 2021/9/2 11:28
 * @Description:
 */
@SuppressWarnings("all")
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    //根据id查询分类信息
    @Override
    public CategoryVo findCategoryById(Long categoryId) {
        if (EmptyUtils.isEmpty(categoryId)){
            return null;
        }
        Category category = categoryMapper.selectById(categoryId);
        CategoryVo categoryVo = copy(category);
        return categoryVo;
    }

    //查询所有分类信息
    @Override
    public List<CategoryVo> findCategoryAll() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Category::getId, Category::getCategoryName);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        List<CategoryVo> categoryVos = copyList(categories);
        return categoryVos;
    }

    //查询所有分类信息
    @Override
    public Result findAllDetail() {
        List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<>());
        List<CategoryVo> categoryVos = copyList(categories);

        return Result.success(categoryVos);
    }

    //通过id查询分类信息
    @Override
    public Result categoriesDetailById(Long id) {
        if (EmptyUtils.isNotEmpty(id)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }
        Category category = categoryMapper.selectById(id);
        CategoryVo categoryVo = copy(category);
        return Result.success(categoryVo);
    }


    @Override
    public Result getCategoryAll(CategoryPageParam params) {
        if (EmptyUtils.isEmpty(params)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }
        Page<Category> page = new Page<>(params.getPageNo(), params.getPageSize());
        IPage<Category> categoryIPage = categoryMapper.categoryList(page, params.getCategoryName(), params.getDescription());
        List<Category> records = categoryIPage.getRecords();
        PageResult<CategoryVo> pageResult = new PageResult<>();
        List<CategoryVo> categoryVos = copyList(records);
        if (EmptyUtils.isNotEmpty(categoryVos)) {
            pageResult.setList(categoryVos);
            pageResult.setTotal(categoryIPage.getTotal());
        }

        return Result.success(pageResult);
    }

    @Override
    public Result deleteCategoryById(Long id) {
        if (EmptyUtils.isEmpty(id)){
            return new Result(999, ErrorCode.ID_NULL.getMsg());
        }
        categoryMapper.deleteById(id);

        return Result.success(null);
    }

    @Override
    public Result UpdateCategoryById(Category category) {
        if (EmptyUtils.isEmpty(category)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }

        categoryMapper.updateById(category);
        return Result.success(null);

    }

    @Override
    public Result addCategory(Category category) {
        if (EmptyUtils.isEmpty(category)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }
        category.setAvatar("/static/category/language.png");
        categoryMapper.insert(category);
        return Result.success(null);
    }

    public List<CategoryVo> copyList(List<Category> categoryList) {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (Category category : categoryList) {
            categoryVoList.add(copy(category));
        }
        return categoryVoList;
    }

    public CategoryVo copy(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        categoryVo.setId(String.valueOf(category.getId()));
        return categoryVo;
    }

}